package com.token.iot.bratterycharging.gateway.utils;

import com.token.iot.bratterycharging.gateway.server.BratteryChargingServerHandler;

public class ByteUtil {

	// _的ascii码，十进制为95
	public final static byte START = 0x5F;

	public final static byte[] END = { 0x0D, 0x0A };

	// 转换成十六进制字符串
	public static String byte2Hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
				hs = hs + "0" + stmp;
			} else {
				hs = hs + stmp;
			}
			if (n < b.length - 1)
				hs = hs + ":";
		}
		return hs.toUpperCase();
	}

	static public byte[] append(byte[] start, byte[] end) {
		byte[] data3 = new byte[start.length + end.length];
		System.arraycopy(start, 0, data3, 0, start.length);
		System.arraycopy(end, 0, data3, start.length, end.length);
		return data3;
	}

	static public byte[] removeFlag(byte[] b) {
		byte[] data3 = new byte[b.length - 1];
		System.arraycopy(b, 1, data3, 0, b.length - 1);
		return data3;
	}

	static public byte[] addFlag(byte[] b) {
		int length = b.length+6;
		String lengthVal ="_"+String.format("%03d", length);
		
		byte[] date = append(lengthVal.getBytes(),b);
		byte[] data3 = append(date,END);
		return data3;
	}

	private static String hexString(byte byteNumber) {
		int toStr = byteNumber;
		if (byteNumber < 0) {
			toStr = byteNumber + 256;
		}
		String byteStr = Integer.toHexString(toStr);
		if (byteStr.length() == 1) {
			byteStr = "0" + byteStr;
		}
		return "0x" + byteStr.toUpperCase();
	}

	public static String toHexString(byte[] block) {
		return arrayShortHexString(block);
	}

	public static String arrayShortHexString(byte[] src, String delim) {
		return arrayHexString(src, delim).replace("0x", "");
	}

	public static String arrayShortHexString(byte[] src) {
		return arrayHexString(src, null).replace("0x", "");
	}

	public static String arrayHexString(byte[] src, String delim) {
		if (delim == null) {
			delim = "";
		}
		if (src == null) {
			return "";
		}
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < src.length; i++) {
			byte byteNumber = src[i];
			sb.append(hexString(byteNumber));
			sb.append(delim);
		}
		String toPrint = sb.toString();
		int start = toPrint.length() - delim.length();
		if (delim.equals(toPrint.substring(start, toPrint.length()))) {
			toPrint = toPrint.substring(0, start);
		}
		return toPrint;
	}

	public static void printByte(byte[] b) {
		System.out.println(toHexString(b));
	}

	public static String bcdToAscii(byte[] bcd) {
		byte[] ascii = new byte[2 * bcd.length];
		for (int i = 0; i < bcd.length; i++) {
			byte hi = (byte) (bcd[i] >> 4);
			byte lo = (byte) (bcd[i] & 0x0F);
			if (hi < 0x00 || hi > 0x09 || lo < 0x00 || lo > 0x09) {
				throw new IllegalArgumentException();
			}
			ascii[2 * i] = ((byte) (hi + 0x30));
			ascii[2 * i + 1] = ((byte) (lo + 0x30));
		}
		return new String(ascii);
	}

	

	public static void main(String[] args) {
		byte[] b = { 95, 80, 71, 65, 88, 84, 48, 48, 48, 48, 48, 48, 48, 49, 53, 51, 49, 44, 48, 35, 47, 35, 48, 35, 47,
				35, 71, 80, 82, 83, 13, 10 };
		String x = byte2Hex(b);
		String xx = new String(b);
		System.out.println(x);
		System.out.println(xx);
		byte[] a = append(b, END);
		System.out.println(b.length);
		System.out.println(a.length);

		printByte(b);
		byte[] c = removeFlag(b);
		printByte(c);

	}
}
